﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Zyg.SSO.Core.Business;
using Zyg.Utility.DBUtility;

namespace Zyg.SSO.Core.Providers.DALProviders
{
    public partial class SqlProvider : DALProvider
    {
        public override Role SelectRole(Guid id)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, id);
            SqlDataReader reader = db.ExecuteSPReader("usp_SelectRole");

            if (reader != null && !reader.IsClosed && reader.Read())
            {
                Role role = new Role();

                if (!reader.IsDBNull(1)) role.Name = reader.GetString(1);
                if (!reader.IsDBNull(2)) role.DateCreated = reader.GetDateTime(2);
                if (!reader.IsDBNull(3)) role.Remark = reader.GetString(3);

                reader.Close();

                return role;
            }
            else
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();

                return null;
            }
        }

        public override void InsertRole(Role role)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, role.Id);
            db.AddParameter("@Name", SqlDbType.VarChar, role.Name);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, role.DateCreated);
            db.AddParameter("@Remark", SqlDbType.VarChar, role.Remark);

            db.ExecuteSP("usp_InsertRole");
        }

        public override void DeleteRole(Role role)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, role.Id);

            db.ExecuteSP("usp_DeleteRole");
        }

        public override void UpdateRole(Role role)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@ID", SqlDbType.UniqueIdentifier, role.Id);
            db.AddParameter("@Name", SqlDbType.VarChar, role.Name);
            db.AddParameter("@DateCreated", SqlDbType.DateTime, role.DateCreated);
            db.AddParameter("@Remark", SqlDbType.VarChar, role.Remark);

            db.ExecuteSP("usp_UpdateRole");
        }

        public override bool ExistsRole(string name)
        {
            SqlServerUtility db = new SqlServerUtility();

            db.AddParameter("@Name", SqlDbType.VarChar, name);

            SqlDataReader reader = db.ExecuteSqlReader("Select [ID] From [Role] Where [Name] = @Name");

            if (reader != null && !reader.IsClosed && reader.Read())
            {
                reader.Close();

                return true;
            }
            else
            {
                if (reader != null && !reader.IsClosed)
                    reader.Close();

                return false;
            }
        }

        public override IList<Role> FillRoles()
        {
            IList<Role> results = new List<Role>();

            SqlServerUtility db = new SqlServerUtility();

            SqlDataReader reader = db.ExecuteSPReader("usp_SelectRolesAll");

            if (reader != null)
            {
                while (reader.Read())
                {
                    Role role = new Role();

                    if (!reader.IsDBNull(0)) role.Id = reader.GetGuid(0);
                    if (!reader.IsDBNull(1)) role.Name = reader.GetString(1);
                    if (!reader.IsDBNull(2)) role.DateCreated = reader.GetDateTime(2);
                    if (!reader.IsDBNull(3)) role.Remark = reader.GetString(3);

                    role.MarkOld();

                    results.Add(role);
                }

                reader.Close();
            }

            return results;
        }
    }
}
